package cn.itcast.bassm.util;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.codec.binary.Base64;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.JsonSyntaxException;

public class rlsbutil {
	
		private String detectFaceServer="http://192.168.1.135:8080/api/v1/tool/detectFaceInFile";
		
	   /**
     * 构造Basic Auth认证头信息
     * 
     * @return
     */
    private String getHeader(String username, String password) {
        String auth = username + ":" + password;
        byte[] encodedAuth = Base64.encodeBase64(auth.getBytes(Charset.forName("US-ASCII")));
        String authHeader = "Basic " + new String(encodedAuth);
        return authHeader;
    }
    
    
    public String doPostFile(String urlStr, String paramName, String filePath) throws ClientProtocolException, IOException {
        String result = null;
        @SuppressWarnings("resource")
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost post = new HttpPost(urlStr);
        post.addHeader("Authorization", getHeader("admin","123456"));
        FileBody fileBody = new FileBody(new File(filePath));
        StringBody stringBody = null;
        try {
            stringBody = new StringBody(paramName);
        } catch (UnsupportedEncodingException e) {
           
        }

        MultipartEntity entity = new MultipartEntity();
        entity.addPart("file", fileBody);
        entity.addPart("desc", stringBody);
        post.setEntity(entity);

        HttpResponse httpResp = httpclient.execute(post);

        if (httpResp.getStatusLine().getStatusCode() == 200) {// 判断是够请求成功
            result = EntityUtils.toString(httpResp.getEntity(), "UTF-8");// 获取返回的数据
        } else {
           System.out.println("请求失败");
        }

        httpclient.getConnectionManager().shutdown();
        return result;
    }
    
    public List<String> detectFace( String imgFile) {
        String feature = null;
        String face_image = null;
        List<String> retList = new ArrayList<String>();
        String json = null;
        try {
            json = doPostFile(detectFaceServer, "file", imgFile);
        } catch (IOException e1) {
           ;
        }

        if (json != null) {
            //System.out.print(json + "\n");
            try {
                JsonParser parser = new JsonParser(); // 创建JSON解析器
                JsonObject object = (JsonObject) parser.parse(json); // 创建JsonObject对象
                int errorcode = object.get("errorCode").getAsInt();
                if (errorcode == 0) {
                    JsonObject dataobject = object.get("data").getAsJsonObject();
                    if (dataobject != null) {
                        JsonArray array = dataobject.get("result").getAsJsonArray();
                        if (array.size() > 0) {
                            JsonObject subObject = array.get(0).getAsJsonObject();

                            feature = subObject.get("feature").getAsString();
                            face_image = subObject.get("face_image").getAsString();
                            retList.add(feature);
                            retList.add(face_image);
                        }
                    }
                } else {
                   System.out.println("返回失败");
                }
            } catch (JsonSyntaxException e) {
                e.printStackTrace();
            }
        } else {
            System.out.println("我也不太清楚");
        }
        return retList;
    }
    
    
}
